package com.winit.need.eventListener;

import com.winit.common.CloudLinkSend.MqMessage;
import com.winit.common.CloudLinkSend.MqMsgType;
import com.winit.common.base.bean.ExecuteResult;
import com.winit.issue.manager.IssueManager;
import com.winit.issue.model.Issue;
import com.winit.issue.repository.IssueRepository;
import com.winit.need.mapper.NeedMapper;
import com.winit.need.model.Need;
import com.winit.need.service.NeedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by wanglin on 2018/4/8.
 */
@Component
@RabbitListener(queues = "need-receive-status-queue")
public class NeedListener {
    private static Logger logger = LoggerFactory.getLogger(NeedListener.class);
    @Autowired
    private NeedMapper needMapper;
    @Autowired
    private IssueManager issueManager;
    @Autowired
    private NeedService needService;
    @Autowired
    private IssueRepository issueRepository;



    @RabbitHandler
    public void process(MqMessage message) {
        Map json = message.getMap();
        MqMsgType type = message.getType();
        logger.info(json.toString());
        switch (type){
            case PRD:
                this.updateNeedStatus((String) json.get("needId"),"PRD");
                break;
            case ISSUE_DEV:
                this.updateNeedStatus((String) json.get("needId"),"ISSUE_DEV");
                break;
            case ONLINE:
                this.updateNeedOnline((String) json.get("issueId"));

        }
    }

    private void updateNeedStatus(String needId,String status){
        Need need = needMapper.selectByPrimaryKey(needId);
        if("PRD".equals(status) && "CONFIRM".equals(need.getStatus())){
            need.setStatus("PRODUCT_DESIGN");
        }else if("ISSUE_DEV".equals(status) && "PRODUCT_DESIGN".equals(need.getStatus())){
            need.setStatus("DEVING");
        }
        needService.updateNeed(need);
    }

    private void updateNeedOnline(String issueId){
        Issue issue = issueRepository.findOne(issueId);


        boolean isTrue = true;
        List<String> needIds = new ArrayList<>();
        if (!StringUtils.isEmpty(issue.getNeedId())) {
            needIds.add(issue.getNeedId());
            List<Issue> issueNeeds = issueManager.findByNeedId(issue.getNeedId());
            for (Issue is : issueNeeds) {
                if (!"COMPLETE".equals(is.getStatus())) {
                    isTrue = false;
                }
            }
        }
        if (isTrue) {
            for (String id : needIds) {
                Need need = new Need();
                need.setId(id);
                need.setActualCompletionTime(new Date());
                need.setStatus("ONLINE");
                needMapper.updateByPrimaryKeySelective(need);
            }
        }
    }


}
